package com.tool.cn.use.search.map;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tool.cn.util.CollectionUtil;
import com.tool.cn.util.IpUtil;
import com.tool.cn.util.StringUtil;

/**
 * 
 * @author wangzonghui
 * @date 2022年4月27日 上午10:47:56 
 * @Description 
 */
public class IpMaskMap {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * key：
	 * value : 
	 */
	private Map<String,List<String>> sourceIpMaskMap=new Hashtable<>();
	/**
	 * key：
	 * value：
	 */
	private Map<String,List<String>> objectiveIpMaskMap=new Hashtable<>();
	
	private Map<String,String> recordMap=new Hashtable<>();
	
	/**
	 * 添加数据
	 * @param sourceIpMask 
	 * @param objectiveIpMask 
	 * @param data 数据
	 * @return
	 */
	public boolean add(String sourceIpMask,String objectiveIpMask, String data) {
		List<String> tmpList;
		try {
			if(sourceIpMaskMap.containsKey(sourceIpMask)) {
				tmpList=sourceIpMaskMap.get(sourceIpMask);
			}else {
				tmpList=new ArrayList<>();
			}
			
			if(!tmpList.contains(objectiveIpMask)) {
				tmpList.add(objectiveIpMask);
				sourceIpMaskMap.put(sourceIpMask, tmpList);
			}
			
			if(objectiveIpMaskMap.containsKey(objectiveIpMask)) {
				tmpList=objectiveIpMaskMap.get(objectiveIpMask);
			}else {
				tmpList=new ArrayList<>();
			}
			
			if(!tmpList.contains(data)) {
				tmpList.add(data);
				objectiveIpMaskMap.put(objectiveIpMask, tmpList);
				
				//添加记录
				recordMap.put(data, sourceIpMask+StringUtil.line+objectiveIpMask);
			}else {
				log.error("Data {} Have Map",data);
			}
			
			
		} catch (Exception e) {
			log.error("Add sourceIpMask {} objectiveIpMask:{}  Of Data:{} Error",sourceIpMask,objectiveIpMask,data);
			return false;
		}
		return true;
	}

	/**
	 * 删除数据
	 * @param sourceIpMask
	 * @param objectiveIpMask 
	 * @param data 数据
	 * @return
	 */
	public boolean remove(String sourceIpMask,String objectiveIpMask,String data) {
		List<String> tmpList;
		try {
			
			if(objectiveIpMaskMap.containsKey(objectiveIpMask)) {
				tmpList=objectiveIpMaskMap.get(objectiveIpMask);
				if(tmpList.removeIf(e -> e.equals(data))) {
					
					if(tmpList.size()>0) {
						objectiveIpMaskMap.put(objectiveIpMask, tmpList);
					}else {
						objectiveIpMaskMap.remove(objectiveIpMask);
						
						if(sourceIpMaskMap.containsKey(sourceIpMask)) {
							tmpList=sourceIpMaskMap.get(sourceIpMask);
							if(tmpList.removeIf(e -> e.equals(objectiveIpMask))) {
								if(tmpList.size()>0) {
									sourceIpMaskMap.put(sourceIpMask, tmpList);
								}else {
									sourceIpMaskMap.remove(sourceIpMask);
								}
								
							}
						}
					}
				}
			}
			
			recordMap.remove(data);
			
		} catch (Exception e) {
			log.error("Remove sourceIpMask {} objectiveIpMask:{}  Of Data:{} Error",sourceIpMask,objectiveIpMask,data);
			return false;
		}
		return true;
	}
	
	/**
	 * 删除数据
	 * @param data
	 * @return
	 */
	public boolean remove(String data) {
		if(recordMap.containsKey(data)) {
			String[] tmp=recordMap.get(data).split("\\"+StringUtil.line);
			return remove(tmp[0], tmp[1], data);
		}
		return true;
	}

	/**
	 * 搜索数据
	 * @param sourceIp 
	 * @param objectiveIp 
	 * @return
	 */
	public List<String> search(String sourceIp,String objectiveIp) {
		
		List<String> dataList=new ArrayList<>();
		List<String> dataTmpList;
		
		for(String key:sourceIpMaskMap.keySet()) {
			if(IpUtil.checkIp(key, sourceIp)) {  //验证源公网ip是否满足掩码ip
				
				dataTmpList=sourceIpMaskMap.get(key);
				for(String one:dataTmpList) {
					if(IpUtil.checkIp(one, objectiveIp)) {  //验证目的ip是否满足掩码ip
						dataTmpList=objectiveIpMaskMap.get(one);
						if(CollectionUtil.isNotEmptyCollection(dataTmpList)) {
							dataList.addAll(dataTmpList);
						}
					}
				}
			}
		}

		
		return dataList;
	}
	
	@Override
	public String toString() {
		if(sourceIpMaskMap.size()>0) {
			return StringUtil.mapToString(sourceIpMaskMap)+" "+StringUtil.mapToString(objectiveIpMaskMap);
		}
		return "";
	}
	
	/**
	 * 获取所有值
	 * @return
	 */
	public Set<String> getValue(){
		return recordMap.keySet();
	}

}
